package com.qf.xiaomi.service.impl;

import com.qf.xiaomi.dao.AddressDao;
import com.qf.xiaomi.dao.GoodsDao;
import com.qf.xiaomi.dao.cartDao;
import com.qf.xiaomi.dao.impl.AddressDaoImpl;
import com.qf.xiaomi.dao.impl.GoodsDaoImpl;
import com.qf.xiaomi.dao.impl.cartDaoImpl;
import com.qf.xiaomi.dao.impl.orderDaoImpl;
import com.qf.xiaomi.dao.orderDao;
import com.qf.xiaomi.pojo.*;
import com.qf.xiaomi.service.orderService;

import java.util.*;

/**
 * Created by 田贵宾 on 2022/8/20 9:46
 *
 * @author 田贵宾
 * @version 1.0
 * //TODO java练习阶段
 * @since 1.0
 */
public class orderServiceImpl implements orderService {

    orderDao dao = new orderDaoImpl();
    AddressDao daoA=new AddressDaoImpl();
    cartDao daoC= new cartDaoImpl();
    GoodsDao daoG= new GoodsDaoImpl();
    @Override
    public List<TbOrder> selectAll(long uid) {

        return dao.selectAll(uid);
    }

    @Override
    public List<TbAddress> selectAddressByUid(long uid) {
        return  daoA.selectAddress(uid);

    }

    @Override
    public Map createOrder(long aid, long uid, TbCartList cart) {
        //创建订单
        long sum=0;
        for (int i = 0; i < cart.getGoodsList().size(); i++) {
            sum+=cart.getGoodsList().get(i).getPrice()*cart.getNums()[i];
        }
        String oid = UUID.randomUUID().toString().replace("-", "");
        dao.createOrder(oid,aid,uid,sum);
        //订单kz
        for (int j = 0; j < cart.getGoodsList().size(); j++) {
            TbGoods tG = cart.getGoodsList().get(j);
            dao.orderDetail(oid,tG.getKzid(),cart.getNums()[j], tG.getPrice()*cart.getNums()[j]);
        }
        //清空购物车
        daoC.clearCart();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id",oid);
        hashMap.put("money",sum);
        return hashMap;
    }

    @Override
    public List<TbOrder> selectOrderByUid(long uid) {

        List<TbOrder> tbOrders = dao.selectOrderByUid(uid);
        tbOrders.forEach(tbOrder -> {
            tbOrder.setAddress(daoA.selectById(tbOrder.getAid()));
        });
        return tbOrders;
    }

    @Override
    public void updateStatus(String oid, String s) {
        dao.updateStatus(oid,s);
    }

    @Override
    public OrderDetail findByOid(OrderDetail order, String oid) {
        TbOrder order1=dao.findByOid(oid);

        order.setStatus(order1.getStatus());
        order.setId(order1.getId());
        order.setCount(order1.getMoney());
        order.setTime(order1.getTime());

        order.setAddress(daoA.selectById(order1.getAid()));
        //根究订单id查找所有的订单详情信息
        List<TbOrderdetail> detail = dao.fingByOid(oid);
        //从订单详情中 去除所有得pid--->kz信息得id
        List<TbGoodsKz> goods = new ArrayList<>();
        for (int i = 0; i < detail.size(); i++) {
             goods.add(i,daoG.findByPid(detail.get(i).getPid()));
        }

        List<OrderItems> itemsList=new ArrayList<>();
        //通过pid去查找所有的kz表信息
        for (int j = 0; j < goods.size(); j++) {
            //创建一个orderItems 对象 用来存放items
            OrderItems orderItems = new OrderItems();
            //根据pid找到第j个数据存放到Product中
           orderItems.setProduct(goods.get(j));
           //将第j个订单详情得num值放到第j个items中得num中
           orderItems.setNum(detail.get(j).getNum());
            //将第j个订单详情得money值放到第j个items中得money中
           orderItems.setCount(detail.get(j).getMoney());
           //将找到得第j组orderItems对象 存入 item数组中
            itemsList.add(orderItems);
        }
        order.setItems(itemsList);
        return order;
    }
}
